package com.probox.common.entity.service.order.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

import com.probox.common.core.enums.PayStatusEnum;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.StringUtils;
import com.probox.customer.api.domain.CustomerGroupDefinitionQuery;
import com.probox.product.api.domain.ProductOptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.order.OrderInfoParentMapper;
import com.probox.common.entity.domain.order.OrderInfoParent;
import com.probox.common.entity.service.order.IOrderInfoParentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单父Service业务层处理
 *
 * @author hds
 * @date 2021-05-27
 */
@Service
@Transactional
public class OrderInfoParentServiceImpl extends ServiceImpl<OrderInfoParentMapper, OrderInfoParent> implements IOrderInfoParentService {
    @Autowired
    private OrderInfoParentMapper orderInfoParentMapper;


    /**
     * 查询订单父
     *
     * @param parentUid 订单父ID
     * @return 订单父
     */
    @Override
    public OrderInfoParent selectOrderInfoParentById(Long parentUid) {
        return this.getById(parentUid);
    }

    @Override
    public OrderInfoParent getOrderInfo(String parentOrderNo) {
        QueryWrapper<OrderInfoParent> queryWrapper = Wrappers.query();
        queryWrapper.eq("PARENT_ORDER_NO",parentOrderNo);
        List<OrderInfoParent> list =  orderInfoParentMapper.selectList(queryWrapper);
        if (list.size()>0)
            return list.get(0);
        return null;
    }

    /**
     * 查询订单父列表
     *
     * @param orderInfoParent 订单父
     * @return 订单父
     */
    @Override
    public List<OrderInfoParent> queryList(OrderInfoParent orderInfoParent) {
        QueryWrapper<OrderInfoParent> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoParent.getParentUid())) && orderInfoParent.getParentUid() != null) {
            queryWrapper.eq("PARENT_UID", orderInfoParent.getParentUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoParent.getParentOrderNo())) && orderInfoParent.getParentOrderNo() != null) {
            queryWrapper.eq("PARENT_ORDER_NO", orderInfoParent.getParentOrderNo());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoParent.getParentAmount())) && orderInfoParent.getParentAmount() != null) {
            queryWrapper.eq("PARENT_AMOUNT", orderInfoParent.getParentAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoParent.getFreightAmount())) && orderInfoParent.getFreightAmount() != null) {
            queryWrapper.eq("FREIGHT_AMOUNT", orderInfoParent.getFreightAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoParent.getPayAmount())) && orderInfoParent.getPayAmount() != null) {
            queryWrapper.eq("PAY_AMOUNT", orderInfoParent.getPayAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoParent.getPayTime())) && orderInfoParent.getPayTime() != null) {
            queryWrapper.eq("PAY_TIME", orderInfoParent.getPayTime());
        }
        return orderInfoParentMapper.selectList(queryWrapper);
    }

    /**
     * 新增订单父
     *
     * @param orderInfoParent 订单父
     * @return 结果
     */
    @Override
    public boolean insertOrderInfoParent(OrderInfoParent orderInfoParent) {

        return this.save(orderInfoParent);

    }

    /**
     * 修改订单父
     *
     * @param orderInfoParent 订单父
     * @return 结果
     */
    @Override
    public boolean updateOrderInfoParent(OrderInfoParent orderInfoParent) {

        return this.updateById(orderInfoParent);
    }

    /**
     * 批量删除订单父
     *
     * @param parentUids 需要删除的订单父ID
     * @return 结果
     */
    @Override
    public boolean deleteOrderInfoParentByIds(Long[] parentUids) {
        return this.removeByIds(Arrays.asList(parentUids));
    }

    /**
     * 根据支付单号查询
     * @param parentOrderNo
     * @return
     */
    @Override
    public OrderInfoParent getParent(String parentOrderNo) {
        if(ObjectUtil.isEmpty(parentOrderNo))
            throw new BaseException("支付单号不能为空");
        QueryWrapper<OrderInfoParent> queryWrapper = Wrappers.query();
        queryWrapper.eq("PARENT_ORDER_NO",parentOrderNo);
        queryWrapper.eq("PAY_STATUS", PayStatusEnum.unpaid.getKey());
        List<OrderInfoParent> parentList = orderInfoParentMapper.selectList(queryWrapper);
        if(ObjectUtil.isNotEmpty(parentList))
            return parentList.get(0);
        return null;
    }

    @Override
    public boolean updatePayStatus(String parentOrderNo) {

        return orderInfoParentMapper.updatePayStatus(parentOrderNo);
    }

    @Override
    public List<ProductOptVo> getOptCount(CustomerGroupDefinitionQuery query) {

        return orderInfoParentMapper.getOptCount(query);
    }


}
